import pandas as pd
import datetime


if __name__ == '__main__':
    #
    from Core.Config import *
    pathfilename = os.getcwd() + "\..\Config\config2.json"
    config = Config(pathfilename)
    database = config.DataBase("JDMySQL")
    realtime = config.RealTime()
    #
    datetime1 = datetime.datetime(2018, 1, 1)
    datetime2 = datetime.datetime(2021, 2, 1)
    #
    df_hs300 = pd.read_csv("d://hs300_instrument_list.csv")
    hs300 = list(df_hs300["Symbol"])
    # hs300 = hs300[:10]

    keep_fields = ["symbol", "datetime", "date", "adjfactor", "trade_status",
                   "open", "high", "low", "close", "bopen", "bhigh", "blow", "bclose",
                   "money", "volume", "total_shares", "free_float_shares"]

    # 输出股票价格数据
    df_list = []
    for symbol in hs300:
        df_tmp = database.GetDataFrame("financial_data", "stock_dailybar",
                                       filter=[("symbol", symbol), ("datetime", ">=", datetime1), ("datetime", "<=", datetime2)],
                                       projection=keep_fields)
        #
        print(symbol, len(df_tmp))
        df_list.append(df_tmp)

    #
    df_price = pd.concat(df_list)
    print(len(df_price))
    df_price.to_csv("e:\\stock_price_hs300.csv", encoding="utf_8_sig")

    # 输出指数价格数据
    df_list = []
    for symbol in ["000300.SH"]:
        df_tmp = database.GetDataFrame("financial_data", "index_dailybar",
                                       filter=[("symbol", symbol), ("datetime", ">=", datetime1), ("datetime", "<=", datetime2)],
                                       projection=keep_fields)
        #
        print(symbol, len(df_tmp))
        df_list.append(df_tmp)
    #
    df_price = pd.concat(df_list)
    print(len(df_price))
    df_price.to_csv("e:\\index_price.csv", encoding="utf_8_sig")